
layui.use(['form', 'laydate'], function() {
	var form = layui.form;
	var laydate = layui.laydate;
	var $ = layui.$;
	var layer = layui.layer;
	laydate.render({
		elem: '#birthday',
		min: '2000-10-10',
		max: 0
	});

	form.verify({
		account: function(value, item) { //value：表单的值、item：表单的DOM对象
			if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
				return '用户名不能有特殊字符';
			}
			if(/(^\_)|(\__)|(\_+$)/.test(value)) {
				return '用户名首尾不能出现下划线\'_\'';
			}
			if(/^\d+\d+\d$/.test(value)) {
				return '用户名不能全为数字';
			}
		},
		pass: [
			/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
		]
	});
	$("#new-StudentRegistForm").hide();
	$("#studentRegistForm").hide();




	$("#studentRegistForm").validate({ //验证前后两次密码
		// onfocusout: function(element) {
		// 	$(element).valid();
		// },
		
		rules: {
			password: {
				required: true,
				minlength: 5,
			},
			confirm_password: {
				required: true,
				minlength: 5,
				equalTo: "#fristWord"
			}
		},
		
		messages: {

			password: {
				required: "请输入密码",
				minlength: "密码长度不能小于 5 个字母"
			},
			confirm_password: {
				required: "请输入密码",
				minlength: "密码长度不能小于 5 个字母",
				equalTo: "两次密码输入不一致"
			}

		}
		
	});
	
//返回按钮
$(".back").click(function(){
	window.location.go(-1);
})
	form.on('submit(getStudentInfo)', function(data) {
		var id_card = $(".idCard").val();
		var stamp = new Date().getTime();
		var param = {
			stamp: stamp,
			id_card: id_card,
			version: 0,
			visit_plat: 0,
			visit_type: 0,
			is_used: 0,
		};
		param = JSON.stringify(param);
		url = path + '/student/getStudentInfo'
		$.ajax({
			url: url,
			data: {
				param: param
			},
			dataType: 'json',
			type: 'post',
			async: true,
			success: function(data) {
				var rows = data.rows;
				var info = data.rows;
				var account = info.account;
				if(data.status == '000') {
					$("#studentRegistForm").show().siblings().hide(); //有信息的显示这个
					$(".haveIdCard").val($(".idCard").val());
					$(".name").val(info.name); //名称
				  //$(".zc-xbxz").val(info.gender): cheched; //性别
					$(".idCard").val(info.idCard); //身份证
//					$(".company").val(info.company); //工作单位
					$(".blood").val(info.blood); //血型
//					$(".liaison").val(info.liaison); //手机号码
					// $(".account").val(info.account); //账号
					// $(".password").val(info.password); //密码
					$(".address").val(info.address); //地址
					$(".gender").val(info.gender);
					if(info.gender == '0') {
						$(".gender").val("男");
					}
					if(info.gender == '1') {
						$(".gender").val("女");
					}
					$(".getInfo").hide();
				}
				if(data.status == '001') {
					$("#new-StudentRegistForm").show().siblings().hide();
					 $(".new-idCard").val($(".idCard").val());
					$(".getInfo").hide();
				}
			}
		});
		return false;
	});

	form.on('submit(saveStudent)', function(data) { //有信息的提交
		if($('.error').text()){
			return false;
		}
		var stamp = new Date().getTime();
		if($(".gender").val() == '男') {
			var gender = '0';
		} else {
			var gender = '1'
		}
		var school = $(".school").val();
		var password = $(".password").val();

		param1 = Object.assign(param, {
			stamp: stamp,
			gender: gender,
			school: school,
			password: password
		}, publicParam)
		param1 = JSON.stringify(param1)
		param.stamp = stamp;
		param = JSON.stringify(param);

		var myUrl = path + "/student/saveStudent";
		$.ajax({
			url: myUrl,
			data: {
				param: param
			},
			dataType: 'json',
			type: 'post',
			async: true,
			success: function(data) {
				
				var rows = data.rows;
				if(data.status == '001') {
					layer.msg("请更换账号重新注册!");
					return false;
				} else if(data.status == '000') {
					layer.msg("注册学员成功!");
					document.getElementById("studentRegistForm").reset();
					window.location.href = 'login.html';
				} else {
					layer.msg("网络异常,稍后重试!");
					window.location.href = 'login.html';
				}

			},
			error: function() {
				layer.msg('注册失败');
			}

		})
		return false;
	});

	//----------------------------没有注册过的逻辑

	$("#new-StudentRegistForm").validate({ //验证前后两次密码
		onfocusout: function(element) {
			$(element).valid();
		},
		rules: {
			password: {
				required: true,
				minlength: 5,
				// equalTo: "#newFristWord"
			},
			confirm_password: {
				required: true,
				minlength: 5,
				// equalTo: "#newConfirmPassword"
				equalTo: "#newFristWord"
			}
		},
		messages: {
			password: {
				required: "请输入密码",
				minlength: "密码长度不能小于 5 个字母"
			},
			confirm_password: {
				required: "请输入密码",
				minlength: "密码长度不能小于 5 个字母",
				equalTo: "两次密码输入不一致"
			}
		}
	});
	form.on('submit(new-saveStudent)', function(data) { //无信息的调教
		if($('.error').text()){
			return false;
		}
		
		var gender = $('input:radio[name="gender"]:checked').val()
//		layer.msg(gender);
		var param = data.field
		var stamp = new Date().getTime();
		var school = $(".new-school").val();
		var password = $(".new-confirm-password").val();
		var blood=$(".new-blood option:selected").val();
//		layer.msg(blood);
        var temp=$(".preViewImage").attr("src");
        if(temp==""||temp==undefined||temp==null){
            layer.msg("头像不能为空");
            return false;
        }

		param1 = Object.assign(param, {
			stamp: stamp,
			school: school,
			password: password,
			gender: gender,
			blood:blood
		}, publicParam)
		param1 = JSON.stringify(param1)
		param.stamp = stamp;
		param = JSON.stringify(param);

		var myUrl = path + "/student/saveStudent";
		$.ajax({
			url: myUrl,
			data: {
				param: param
			},
			dataType: 'json',
			type: 'post',
			async: true,
			success: function(data) {
				
				var rows = data.rows;
				if(data.status == '001') {
					layer.msg("请更换账号重新注册!");
				};
				if(data.status == '000') {
					layer.msg("注册学员成功!");
					document.getElementById("studentRegistForm").reset();
//					window.location.href = 'login.html';
                    $("#touxiang").attr("src","");
                    location.href="login.html"
				}

			},
			error: function() {
				layer.msg('注册失败')
			}

		})
		return false;
	});
});




layui.use(['layer', 'upload'], function() {
	var stamp = new Date().getTime();
	var layer = layui.layer;
	var param = {
		stamp: new Date().getTime(),
		version: 0,
		visit_plat: 0,
		visit_type: 0,
		is_used: 0,
	};
	param = JSON.stringify(param);
	var upload = layui.upload;
	var url = path + "/student/upHeadRegister";
	var uploadInst = upload.render({
		elem: '.import', //绑定元素
		method: 'post',
		url: url, //上传接口
		data: {
			param: param
		},
		size: 5000,
		accept: 'images', //允许上传的文件
		async: false,
		done: function(res) {
			if(res.status == '000') {
				var href = res.rows;
				layer.msg("上传成功");
				$(".preViewImage").attr('src', href);
				$(".headUrl").val(href);

			}
		},
		error: function(data) {
			layer.msg("上传失败,请重新上传!")
		}
	});
});

//---------------------------------
//验证身份证
var IDreg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
//	手机号正则
var phoneReg = /^[1][3,4,5,7,8,6,9][0-9]{9}$/;



//验证用户名
function checkAccount(value) { //value：表单的值、item：表单的DOM对象
	if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
		layer.msg('请填写有效的用户名！');
		return false;
	}
	if(/(^\_)|(\__)|(\_+$)/.test(value)) {
		layer.msg('用户名首尾不能出现下划线\'_\'');
		return false;
	}
	if(/^\d+\d+\d$/.test(value)) {
		layer.msg('用户名不能全为数字');
		return false;
	}
	return true;
}


